package <%=basePackage%>.web;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.quidsi.core.platform.exception.ResourceNotFoundException;
import com.tianxian.common.web.BaseController;
import com.tianxian.common.web.exception.InvalidParameterException;
import com.tianxian.common.web.view.EntityListView;
import com.tianxian.common.web.view.EntityView;
import <%=basePackage%>.domain.<%=simpleClassName%>;
import <%=basePackage%>.service.<%=simpleClassName%>Service;

@Controller
@RequestMapping("<%=entity%>")
public class <%=simpleClassName%>Controller extends BaseController {
	@Inject
    private <%=simpleClassName%>Service <%=entity%>Service;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public EntityListView<<%=simpleClassName%>> list<%=simpleClassName%>s(@RequestParam(defaultValue = "0") Integer offset, @RequestParam(defaultValue = "24") Integer fetchSize) {
        List<<%=simpleClassName%>> <%=entity%>s = <%=entity%>Service.list<%=simpleClassName%>s(offset, fetchSize);

        EntityListView<<%=simpleClassName%>> <%=entity%>sView = new EntityListView<<%=simpleClassName%>>();
        <%=entity%>sView.setStatus(STATUS_SUCCESS);
        <%=entity%>sView.setData(<%=entity%>s);
        <%=entity%>sView.setOffset(offset);

        <%=entity%>sView.setFetchSize(fetchSize);
        <%=entity%>sView.setTotal(<%=entity%>Service.get<%=simpleClassName%>TotalSize());
        return <%=entity%>sView;
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public EntityView<<%=simpleClassName%>> update<%=simpleClassName%>(@Valid <%=simpleClassName%> <%=entity%>Form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new InvalidParameterException(bindingResult.getAllErrors());
        }

        if (<%=entity%>Form.getId() == null) {
            // Create
            <%=simpleClassName%> <%=entity%> = new <%=simpleClassName%>();
            <%=entity%>.setCreateTime(new Date());
            <%=entity%>.setLastUpdateTime(new Date());
            <%=entity%>.setStatus(1);

            this.<%=entity%>Service.save<%=simpleClassName%>(<%=entity%>);

            EntityView<<%=simpleClassName%>> <%=entity%>View = new EntityView<<%=simpleClassName%>>();
            <%=entity%>View.setStatus(STATUS_SUCCESS);
            <%=entity%>View.setData(<%=entity%>);

            return <%=entity%>View;
        } else {
            // Update
            <%=simpleClassName%> <%=entity%> = this.<%=entity%>Service.get<%=simpleClassName%>ById(<%=entity%>Form.getId());

            if (<%=entity%> == null) {
               throw new ResourceNotFoundException(null);
            } else {
                //TODO: check fields to update
                <%for(f : fields){%><%if(!f.generated)%>
                <%=entity%>.<%=f.setter%>(<%=entity%>Form.<%=f.getter%>());
                <%}%><%}%>
                <%=entity%>.setLastUpdateTime(new Date());
                this.<%=entity%>Service.update<%=simpleClassName%>(<%=entity%>);
                EntityView<<%=simpleClassName%>> <%=entity%>View = new EntityView<<%=simpleClassName%>>();
                <%=entity%>View.setStatus(STATUS_SUCCESS);
                <%=entity%>View.setData(<%=entity%>);

                return <%=entity%>View;
            }
        }
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public EntityView<<%=simpleClassName%>> get<%=simpleClassName%>(@RequestParam Long id) {
        if (id == null) {
            throw new ResourceNotFoundException(null);
        }
        // Update
        <%=simpleClassName%> <%=entity%> = this.<%=entity%>Service.get<%=simpleClassName%>ById(id);

        if (<%=entity%> == null) {
            throw new ResourceNotFoundException(null);
        } else {
            EntityView<<%=simpleClassName%>> <%=entity%>View = new EntityView<<%=simpleClassName%>>();
            <%=entity%>View.setStatus(STATUS_SUCCESS);
            <%=entity%>View.setData(<%=entity%>);
            return <%=entity%>View;
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public EntityView<<%=simpleClassName%>> delete<%=simpleClassName%>(@RequestParam Long id) {
    	if (id == null) {
           throw new ResourceNotFoundException(null);
        }
        // Update
        <%=simpleClassName%> <%=entity%> = this.<%=entity%>Service.get<%=simpleClassName%>ById(id);

        if (<%=entity%> == null) {
           throw new ResourceNotFoundException(null);
        } else {
            this.<%=entity%>Service.delete<%=simpleClassName%>(id);

            EntityView<<%=simpleClassName%>> <%=entity%>View = new EntityView<<%=simpleClassName%>>();
            <%=entity%>View.setStatus(STATUS_SUCCESS);
            return <%=entity%>View;
        }
    }

}
